﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Citi.ReportManager.Domain
{
    public class HightlightedReport
    {
        public int Id { get; set; }
        public string Soeid { get; set; }
        public int ReportFolderId { get; set; }
        public Category Category { get; set; }
        public string ReportPath { get; set; }
        public DateTime Date { get; set; }

        public IEnumerable<HightlightedReport> SelectRecentlyAdded(int takeResultsNumber)
        {
            return new Report().SelectAll<Report>()
                .OrderByDescending(x => x.InsertDate)
                .Take(takeResultsNumber)
                .Select(x => new HightlightedReport
                {
                    Id = x.Id,
                    Date = x.InsertDate,
                    ReportFolderId = x.ReportFolderId,
                    ReportPath = x.ReportName,
                    Category = x.ReportFolder.Category
                });
        }

        public IEnumerable<HightlightedReport> SelectMostDownload(int takeResultsNumber)
        {
            return new ReportRequest().SelectAll<ReportRequest>()
                .GroupBy(x => new { x.CategoryId, x.ReportName, x.Category })
                .OrderByDescending(x => x.Count())
                .Select(x => new HightlightedReport
                {
                    ReportFolderId = x.Key.CategoryId,
                    ReportPath = x.Key.ReportName,
                    Category = x.Key.Category
                }).Take(takeResultsNumber);
        }

        public IEnumerable<HightlightedReport> SelectFavorites(int takeResultsNumber, string soeid)
        {
            return new Favorite().SelectAll<Favorite>().Where(x => x.Soeid.ToUpper() == soeid.ToUpper())
                .Take(takeResultsNumber).Select(x => new HightlightedReport { Category = x.Category, ReportFolderId = x.CategoryId });
        }

        public IEnumerable<HightlightedReport> SelectCategoryMostDownloadByUser(int takeResultsNumber, string soeid)
        {
            return new ReportRequest().SelectAll<ReportRequest>()
                .Where(x => x.Soeid == soeid.ToUpper())
                .GroupBy(x => new { x.CategoryId, x.Category })
                .OrderByDescending(x => x.Count())
                .Take(takeResultsNumber)
                .Select(x => new HightlightedReport
                {
                    ReportFolderId = x.Key.CategoryId,
                    Category = x.Key.Category
                });
        }

        public IEnumerable<HightlightedReport> SelectDownloadedRecent(int takeResultsNumber)
        {
            return new ReportRequest()
                .SelectAll<ReportRequest>()
                .OrderByDescending(x => x.RequestDate)
                .Take(takeResultsNumber)
                .Select(x => new HightlightedReport
                {
                    Soeid = x.Soeid,
                    Id = x.Id,
                    Date = x.RequestDate,
                    ReportFolderId = x.CategoryId,
                    ReportPath = x.ReportName,
                    Category = x.Category
                });
        }

        public IEnumerable<HightlightedReport> SelectDownloadedRecentByUser(int takeResultsNumber, string soeid)
        {
            return new ReportRequest()
                .SelectAll<ReportRequest>()
                .Where(x => x.Soeid == soeid.ToUpper())
                .OrderByDescending(x => x.RequestDate)
                .Take(takeResultsNumber)
                .Select(x => new HightlightedReport
                {
                    Soeid = x.Soeid,
                    Id = x.Id,
                    Date = x.RequestDate,
                    ReportFolderId = x.CategoryId,
                    ReportPath = x.ReportName,
                    Category = x.Category
                });
        }
    }
}
